<?php
/**
 * Created by yd
 * User: xiaojun.lan
 * Date: 2018/5/15
 * Time: 9:27
 */


namespace app\worker\controller;

use \GatewayWorker\Lib\Gateway;
use think\Db;
use think\Controller;
use think\Config;

class Index extends Controller
{

    /**
     * 模板webSocket界面
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');

    }

    /**
     * 初始化webSocket用户
     */
    public function initUser()
    {
        $uid = input('uid');
        $client_id = input('client_id');

        if(!IS_WIN){
            $registerIpPort  = Config::get('register_ip_port');
        }else{
            $registerIpPort = '127.0.0.1:1236'; // 测试开发用
        }
        Gateway::$registerAddress = $registerIpPort;
        Gateway::bindUid($client_id, $uid);

    }

    /**
     * 客户端post发送信息，服务器webSocket回复例子
     * @return string
     */
    public function doSomeThing()
    {

        $request = request();
        $uid = input('post.uid');
        if (!$uid) {
            $uid = 1;
        }

        $json = json_encode(array('msg' => 'doSomeThing return!', 'get' => $request->get(), 'post' => $request->post()));

        if (!is_array($json)) {
            if(!IS_WIN){
                $registerIpPort  = Config::get('register_ip_port');
            }else{
                $registerIpPort = '127.0.0.1:1236';
            }
            Gateway::$registerAddress = $registerIpPort;

            Gateway::sendToAll($json);
//              Gateway::sendToClient($client_id,$json);
            Gateway::sendToUid($uid, $json);
            Gateway::sendToGroup($uid, $json);

        }

        return $json;
    }

}

